#include"sort.h"


//插入排序
//最坏情况下时间复杂度为O(N^2)，最好是O(N)
void InsertSort(int* a, int n)//n为数组大小
{
	for (int i = 0; i < n - 1; i++)
	{
		int end = i;
		int tmp = a[end + 1];
		while (end >= 0)
		{
			//从小向大排序
			if (tmp < a[end])
			{
				a[end + 1] = a[end];
				end--;
			}
			else
			{
				break;
			}
		}
		a[end + 1] = tmp;
	}
}


//希尔排序
//实际上就是间隔插入排序
//gap越大，大的数会更快的到后面，小的更快的到前面，越不接近有序
//gap越小，数据跳动越慢，越接近有序
//O(N^1.3)
void ShellSort(int* a, int n)
{
	int gap = n;
	while (gap > 1)//实现多次排序
	{
		gap = gap / 3 + 1;//+1为了保证最后一定是一步
		for (int i = 0; i < n - gap; i++)//单次排序
		{
			int end = i;
			int tmp = a[end + gap];
			while (end >= 0)
			{
				if (tmp < a[end])
				{
					a[end + gap] = a[end];
					end -= gap;
				}
				else
				{
					break;
				}
			}
			a[end + gap] = tmp;
		}
	}
}


//堆排序
//O(NlogN)
void Swap(int* x, int* y)
{
	int tmp = *x;
	*x = *y;
	*y = tmp;
}
void AdjustDown(int* a, int n, int parent)
{
	assert(a);
	int child = parent * 2 + 1;
	while (child < n)
	{
		if (child + 1 < n && a[child + 1] > a[child])
		{
			child++;
		}
		if (a[child] > a[parent])
		{
			Swap(&a[child], &a[parent]);
			parent = child;
			child = parent * 2 + 1;
		}
		else
		{
			break;
		}
	}
}
void HeapSort(int* a, int n)
{
	for (int i = (n - 1 - 1) / 2; i >= 0; i--)
	{
		AdjustDown(a, n, i);
	}
	int end = n - 1;
	while (end > 0)
	{
		Swap(&a[0], &a[end]);
		AdjustDown(a, end, 0);
		end--;
	}
}


//选择排序
//O(N^2)
void SelectSort(int* a, int n)
{
	int begin = 0, end = n - 1;
	while (begin < end)
	{
		int mini = begin, maxi = begin;
		for (int i = begin + 1; i <= end; i++)//循环找出最大值与最小值位置
		{
			if (a[i] > a[maxi])
			{
				maxi = i;
			}
			if (a[i] < a[mini])
			{
				mini = i;
			}
		}
		Swap(&a[begin], &a[mini]);//将最小的放在前面
		if (maxi == begin)
		{
			maxi = mini;
		}
		Swap(&a[end], &a[maxi]);//最大的放在后面
		begin++;
		end--;
	}
}


//冒泡排序
//O（N^2）
void BubbleSort(int* a, int n)
{
	for (int j = 0; j < n; j++)
	{
		int exchange = 0;
		for (int i = 1; i < n - j; i++)
		{
			if (a[i - 1] > a[i])
			{
				Swap(&a[i - 1], &a[i]);
				exchange = 1;
			}
		}
		//如果某一趟排序没有发生交换，那么已经有序了
		if (exchange == 0)
		{
			break;
		}
	}
}

int GetmidIndex(int* a, int begin, int end)
{
	int mid = (begin + end) / 2;
	if (a[begin] < a[mid])
	{
		if (a[mid] < a[end])
		{
			return mid;
		}
		else if (a[begin] > a[end])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
	else // a[begin] > a[mid]
	{
		if (a[mid] > a[end])
		{
			return mid;
		}
		else if (a[begin] < a[end])
		{
			return begin;
		}
		else
		{
			return end;
		}
	}
}

//快排
//①霍尔方法：分割出左右区间，key固定位置，左小右大。
//左边做key，右边先走，保证相遇位置处值比key小
//最好O(NlogN)，当每次key都是最大或者最小值(有序时)时间复杂度为O（N^2）
int PartSort1(int* a, int begin, int end)
{
	int mid = GetmidIndex(a, begin, end);
	Swap(&a[begin], &a[mid]);//使得key是数组中的中间值，避免有序时的排序消耗

	int left = begin, right = end;
	int keyi = left;
	while (left < right)
	{
		//右边先走，找小
		while (left < right && a[right] >= a[keyi])
		{
			right--;
		}
		//左边后走，找大
		while (left > right && a[left] <= a[keyi])
		{
			left++;
		}
		Swap(&a[left], &a[right]);
	}
	Swap(&a[left], &a[keyi]);//此时left=right
	keyi = left;
	return keyi;
}

//挖坑法
//首先选取一个key值
int PartSort2(int* a, int begin, int end)
{
	int mid = GetmidIndex(a, begin, end);
	Swap(&a[begin], &a[mid]);
	int left = begin, right = end;
	int key = a[left];
	int hole = left;
	while (left < right)
	{
		//右边找小，填左边
		while (left < right && a[right] >= key)
		{
			right--;
		}
		a[hole] = a[right];
		hole = right;
		//左边找大，填右边
		while (left < right && a[left] <= key)
		{
			left++;
		}
		a[hole] = a[left];
		hole = left;
	}
	a[hole] = key;//退出循环时，left=right
	return hole;
}

//前后指针法
int PartSort3(int* a, int begin, int end)
{
	int prev = begin, cur = begin + 1;
	int keyi = begin;
	while (cur <= end)
	{
		//cur一直向后走，遇到比a[keyi]小的值，则交换数据
		if (a[cur] < a[keyi] && ++prev != cur)
		{
			Swap(&a[prev], &a[cur]);
		}
		cur++;
	}
	Swap(&a[prev], &a[keyi]);
	keyi = prev;
	return keyi;
}




void QuickSort(int* a, int begin, int end)
{
	if (begin >= end)
	{
		return;
	}
	//由于是递归调用，所以如果是小区间排序的话，递归占用资源过多
	//小区间采用直接插入排序，减少递归调用次数
	if ((end - begin + 1) < 15)
	{
		InsertSort(a + begin, end - begin + 1);
	}
	else
	{
		//int keyi=PartSort1(a,begin,end);
		//int keyi = PartSort2(a, begin, end);
		int keyi = PartSort3(a, begin, end);
		QuickSort(a, begin, keyi - 1);
		QuickSort(a, keyi + 1, end);
	}
}

#include"Stack.h"
//快排非递归实现
//通过栈直接改成循环
//栈中存储排序位置的下标，通过下标位置进行排序操作
void QuickSortNonR(int* a, int begin, int end)
{
	ST st;
	StackInit(&st);
	StackPush(&st, begin);
	StackPush(&st, end);
	while (!StackEmpty(&st))
	{
		int right = StackTop(&st);
		StackPop(&st);
		int left = StackTop(&st);
		StackPop(&st);
		int keyi = PartSort1(a, left, right);
		//先右排序
		if (keyi + 1 < right)
		{
			StackPush(&st, keyi + 1);
			StackPush(&st, right);
		}
		//后左排序
		if (left < keyi - 1)
		{
			StackPush(&st, left);
			StackPush(&st, keyi - 1);
		}
	}
	StackDestory(&st);
}


//时间复杂度O(NlogN)
//空间复杂度O(N)
void _MergeSort(int* a, int begin, int end, int* tmp)
{
	if (begin >= end)
		return;

	int mid = (begin + end) / 2;
	// [begin, mid] [mid+1, end] 递归让子区间有序
	_MergeSort(a, begin, mid, tmp);
	_MergeSort(a, mid + 1, end, tmp);

	// 归并[begin, mid] [mid+1, end]
	//...

	int begin1 = begin, end1 = mid;
	int begin2 = mid + 1, end2 = end;
	int i = begin;
	while (begin1 <= end1 && begin2 <= end2)
	{
		if (a[begin1] <= a[begin2])
		{
			tmp[i++] = a[begin1++];
		}
		else
		{
			tmp[i++] = a[begin2++];
		}
	}

	while (begin1 <= end1)
	{
		tmp[i++] = a[begin1++];
	}

	while (begin2 <= end2)
	{
		tmp[i++] = a[begin2++];
	}

	memcpy(a + begin, tmp + begin, sizeof(int) * (end - begin + 1));
}
void MergeSort(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		printf("malloc fail\n");
		exit(-1);
	}
	_MergeSort(a, 0, n - 1, tmp);
	free(tmp);
	tmp = NULL;
}


void MergeSortNonR(int* a, int n)
{
	int* tmp = (int*)malloc(sizeof(int) * n);
	if (tmp == NULL)
	{
		printf("malloc fail/n");
		exit(-1);
	}

	//归并每组数据个数，从1开始
	int gap = 1;
	while (gap < n)
	{
		for (int i = 0; i < n; i += gap * 2)
		{
			//除了begin1=i不会越界，剩余的三个变量都是有可能越界的
			int begin1 = i, end1 = i + gap - 1;
			int begin2 = i + gap, end2 = i + 2 * gap - 1;
			int j = i;

			//越界修正
			if (end1 >= n)
			{
				end1 = n - 1;
				//范围一已经包括所有，begin2-end2区间不存在
				begin2 = n;
				end2 = n - 1;
			}
			else if (begin2 >= n)
			{
				//不存在区间
				begin2 = n;
				end2 = n - 1;
			}
			else if (end2 >= n)
			{
				end2 = n - 1;
			}
			while (begin1 <= end1 && begin2 <= end2)
			{
				if (a[begin1] < a[begin2])
				{
					tmp[j] = a[begin1];
					j++;
					begin1++;
				}
				else
				{
					tmp[j] = a[begin2];
					j++;
					begin2++;
				}
			}
			while (begin1 <= end1)
			{
				tmp[j] = a[begin1];
				j++;
				begin1++;
			}
			while (begin2 <= end2)
			{
				tmp[j] = a[begin2];
				j++;
				begin2++;
			}
		}
		//整体归并完了，再拷贝
		memcpy(a, tmp, sizeof(int) * n);
		gap *= 2;
	}

	free(tmp);
	tmp = NULL;
}

//时间复杂度：O(N+range)
//空间复杂度：O(range)
void CountSort(int* a, int n)
{
	int max = a[0], min = a[0];
	for (int i = 1; i < n; i++)
	{
		if (a[i] > max)
		{
			max = a[i];
		}
		if (a[i] < min)
		{
			min = a[i];
		}
	}
	int range = max - min + 1;
	int* count = (int*)calloc(range,sizeof(int));
	if (count == NULL)
	{
		perror("malloc fail\n");
		exit(-1);
	}
	//统计次数,将次数保存在count数组中
	for (int i = 0; i < n; i++)
	{
		count[a[i] - min]++;
	}
	//再将数字依次排序
	int k = 0;
	for (int j = 0; j < range; j++)
	{
		while (count[j]--)//while循环使得count数组中每个位置都存放完毕
		{
			a[k++] = j + min;
		}
	}
	free(count);
}